package com.xmu.prcs.action;


import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.struts2.json.annotations.JSON;

import com.xmu.prcs.action.bean.KindShuBean;
import com.xmu.prcs.service.ForumManager;
import com.xmu.prcs.service.PlantInfoManager;
import com.xmu.prcs.service.SystemManager;
import com.xmu.prcs.service.UserManager;
import com.xmu.prcs.util.StringHelper;

/**
 * Ajax专用Action
 * @author frequent
 * @version 1.0,2012-3-29
 *
 */
@SuppressWarnings("serial")
public class AjaxAction extends BaseAction{

	private UserManager userManager;
	
	private PlantInfoManager plantInfoManager;
	
	private SystemManager systemManager;
	
	private ForumManager forumManager;
	
	/**
	 * 接受和传回前台Ajax的三个变量
	 * fieldId，表单ID，
	 * fieldValue，传进来时，为表单值，返回时如果用于验证，true或false，
	 * extraData，返回额外信息，
	 */
	private String fieldId;
	
	private String fieldValue;
	
	private String extraData;
	
	/**
	 * 分类科、属
	 */
	private String kindKe;
	
	private String kindshu;

	/**
	 * 根据科动态改变属类表
	 */
	private List<KindShuBean> kindShu;
	
	/**
	 * 增加分类时，判断分类科或属是否存在
	 */
	private String isKindKeExit;
	
	private String isKindShuExit;
	
	/**
	 * 登录时的用户名和密码
	 */
	private String userName;
	
	private String password;
	
	/**
	 * 专题名
	 */
	private String subjectName;
	
	/**
	 * 专题Id
	 */
	private int idSubject;
	
	/**
	 * 专题名唯一性检查结果
	 */
	private int subjectNameCheckResult;
	
	/**
	 * 登录判断的结果
	 */
	public String loginResult;
	
	/**
	 * 话题名唯一性检查结果
	 */
	private int topicNameCheckResult;
	
	/**
	 * 话题名
	 */
	private String topicName;
	
	/**
	 * 帖子名
	 */
	private String postName;
	
	private int userId;
	
	/**
	 * 给用户增加鲜花或砸鸡蛋处理结果
	 */
	private int addFlowerOrEggResult;
	
	@JSON(serialize=false)
	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	@JSON(serialize=false)
	public PlantInfoManager getPlantInfoManager() {
		return plantInfoManager;
	}

	public void setPlantInfoManager(PlantInfoManager plantInfoManager) {
		this.plantInfoManager = plantInfoManager;
	}

	@JSON(serialize=false)
	public SystemManager getSystemManager() {
		return systemManager;
	}

	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}

	@JSON(serialize=false)
	public ForumManager getForumManager() {
		return forumManager;
	}

	public void setForumManager(ForumManager forumManager) {
		this.forumManager = forumManager;
	}

	public String getFieldId() {
		return fieldId;
	}

	public void setFieldId(String fieldId) {
		this.fieldId = fieldId;
	}

	public String getFieldValue() {
		return fieldValue;
	}

	public void setFieldValue(String fieldValue) {
		this.fieldValue = fieldValue;
	}

	public String getExtraData() {
		return extraData;
	}

	public void setExtraData(String extraData) {
		this.extraData = extraData;
	}
	
	public String getKindKe() {
		return kindKe;
	}

	public void setKindKe(String kindKe) {
		this.kindKe = kindKe;
	}

	public List<KindShuBean> getKindShu() {
		return kindShu;
	}

	public void setKindShu(List<KindShuBean> kindShu) {
		this.kindShu = kindShu;
	}

	public String getIsKindKeExit() {
		return isKindKeExit;
	}

	public void setIsKindKeExit(String isKindKeExit) {
		this.isKindKeExit = isKindKeExit;
	}

	public String getIsKindShuExit() {
		return isKindShuExit;
	}

	public void setIsKindShuExit(String isKindShuExit) {
		this.isKindShuExit = isKindShuExit;
	}

	public String getKindshu() {
		return kindshu;
	}

	public void setKindshu(String kindshu) {
		this.kindshu = kindshu;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	

	public String getLoginResult() {
		return loginResult;
	}

	public void setLoginResult(String loginResult) {
		this.loginResult = loginResult;
	}

	public String getSubjectName() {
		return subjectName;
	}

	public void setSubjectName(String subjectName) {
		this.subjectName = subjectName;
	}

	public int getIdSubject() {
		return idSubject;
	}

	public void setIdSubject(int idSubject) {
		this.idSubject = idSubject;
	}

	public int getSubjectNameCheckResult() {
		return subjectNameCheckResult;
	}

	public void setSubjectNameCheckResult(int subjectNameCheckResult) {
		this.subjectNameCheckResult = subjectNameCheckResult;
	}

	public int getTopicNameCheckResult() {
		return topicNameCheckResult;
	}

	public void setTopicNameCheckResult(int topicNameCheckResult) {
		this.topicNameCheckResult = topicNameCheckResult;
	}

	public String getTopicName() {
		return topicName;
	}

	public void setTopicName(String topicName) {
		this.topicName = topicName;
	}

	public String getPostName() {
		return postName;
	}

	public void setPostName(String postName) {
		this.postName = postName;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public int getAddFlowerOrEggResult() {
		return addFlowerOrEggResult;
	}

	public void setAddFlowerOrEggResult(int addFlowerOrEggResult) {
		this.addFlowerOrEggResult = addFlowerOrEggResult;
	}

	/**
     * 检查用户注册时填入的用户名是否是系统中唯一的
     * @return
     */
    public String checkUserName(){
		try {
			//fieldValue为传进来的表单值
			try {
				fieldValue = new String(fieldValue.getBytes("ISO8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return INPUT;
			}
			boolean result = userManager.isUserNameExist(fieldValue);

			if (result) {// 系统中已经存在该用户名
				fieldValue = "true";
				System.out.println("exist");
			} else {// 系统中不存在该该用户名，可以使用
				fieldValue = "false";
				System.out.println("noexist");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Throwable t) {
			t.printStackTrace();
		}
		return SUCCESS;
    }
    
    /**
     * 检查用户注册时填入的邮箱地址是否是系统中唯一的
     * @return
     */
	public String checkEmail() {
		try {
			boolean result = false;
			//判断输入的地址是否是有效的邮箱地址
			System.out.println(fieldValue);
			if(fieldValue == null || fieldValue.equals("") || !StringHelper.isMatchEmail(fieldValue)){
				extraData = "notEmail";
				System.out.println("not a email address...");
			}else{
				result = userManager.isEmailExist(fieldValue);
			}
			if (result) {// 系统中已经存在该邮箱地址了
				fieldValue = "true";
				System.out.println("email exist.....");
			} else {// 系统中不存在该邮箱地址，可以使用
				fieldValue = "false";
				System.out.println("email can  be used");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 检查增加植物时输入的植物名是否唯一性
	 * @return
	 */
	public String checkPlantName(){
		try {
			fieldValue = new String(fieldValue.getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return INPUT;
		}
		System.out.println("fieldvalue: " + fieldValue);
		boolean result = plantInfoManager.isPlantNameExist(fieldValue);
		if(result){//系统中存在此植物名
			fieldValue = "true";
		}else{//该植物名可以使用
			fieldValue = "false";
		}
		return SUCCESS;
	}
	
	/**
	 * 检查增加植物时输入的植物学名是否唯一性
	 * @return
	 */
	public String checkScientificName(){
		try {
			fieldValue = new String(fieldValue.getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return INPUT;
		}
		boolean result = plantInfoManager.isScientificNameExist(fieldValue);
		if(result){//系统中存在此学名
			fieldValue = "true";
		}else{//该学名可以使用
			fieldValue = "false";
		}
		return SUCCESS;
	}
	
	/**
	 * 得到某个分类科下的所有分类属
	 * @return
	 */
	public String listKindShuByKindKe(){
		try {
			kindKe = new String(kindKe.getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return INPUT;
		}
		kindShu = plantInfoManager.getKindShuByKindKe(kindKe);
		
		return "listKindShuByKindKe";
	}
	
	/**
	 * 增加分科科
	 * @return
	 */
	public String addKindKe(){
		try {
			kindKe = new String(kindKe.getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return INPUT;
		}
		if(plantInfoManager.addKindKe(kindKe)){
			isKindKeExit = "true";
		}else{
			isKindKeExit = "false";
		}
		
		kindKe = plantInfoManager.getKindKeByProperty(kindKe).getIdKindKe().toString();
		return "addKindKe";
	}
	
	/**
	 * 增加分类属
	 * @return
	 */
	public String addKindShu(){
		try {
			kindshu = new String(kindshu.getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}
		if(plantInfoManager.addKindShu(kindKe, kindshu)){
			isKindShuExit = "true";
		}else{
			isKindShuExit = "false";
		}
		
		kindshu = plantInfoManager.getKindShuByProperty(kindshu).getIdKindShu().toString();
		return "addKindShu";
	}
	
	/**
	 * 登录时，判断是否登录成功
	 * @return
	 */
	public String login(){
		try {
			try {
				userName = new String(userName.getBytes("ISO8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return INPUT;
			}
			System.out.println("name:" + userName + ",password:" + password);
			loginResult = Integer.toString(systemManager.loginToSystem(userName, password));
		}catch(Exception e){
			e.printStackTrace();
		}
		return "login";
	}
	
	/**
	 * 检查专题的名称唯一性
	 * @return
	 */
	public String checkSubjectName(){
		try {
			subjectName = new String(subjectName.getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return INPUT;
		}
		System.out.println("subjectName:" + subjectName);
		subjectNameCheckResult = 0;
		if(plantInfoManager.isSubjectNameExist(subjectName, idSubject)){
			subjectNameCheckResult = 1;
		}
		
		return "checkSubjectName";
	}
	
	/**
	 * 检查话题的名称唯一性
	 * @return
	 */
	public String checkTopicName(){
		try {
			topicName = new String(topicName.getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return INPUT;
		}
		System.out.println("topicName 2: " + topicName);
		topicNameCheckResult = 0;
		if(forumManager.isTopicNameExist(topicName)){
			topicNameCheckResult = 1;
		}
		return "checkTopicName";
	}
	
	/**
	 * 验证帖子名的唯一性
	 * @return
	 */
	public String checkPostName(){
		try {
			postName = new String(postName.getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return INPUT;
		}
		topicNameCheckResult = 0;
		if(forumManager.isPostNameExist(postName)){
			topicNameCheckResult = 1;
		}
		return "checkPostName";
	}
	
	/**
	 * kindEditor的map插件
	 * @return
	 */
	public String kindEditorMap(){
		return SUCCESS;
	}
	
	/**
	 * 为用户增加鲜花数
	 * @return
	 */
	public String addFlower4User(){
		userManager.addFlower4User(userId);
		addFlowerOrEggResult = 1;
		return "addFlower4User";
	}
	
	/**
	 * 向用户砸鸡蛋
	 * @return
	 */
	public String addEgg4User(){
		userManager.addEgg4User(userId);
		addFlowerOrEggResult = 1;
		return "addEgg4User";
	}
}
